package com.hsbc.dev.teamo4.sms.common.utils.factory;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author:Kevin
 * @version:
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.common.utils.factory
 * @Description: DBConnectFactory
 * @Date date: 2018/7/26
 */
public class DBConnectFactory {
    private static ComboPooledDataSource  dataSource;
    private static Connection connection;

    /**
     * getDataSource
     * @return ComboPooledDataSource
     */
    private static ComboPooledDataSource getDataSource(){
        if(dataSource!=null){
            return dataSource;
        }
        return dataSource=new ComboPooledDataSource("dev");
    }

    /**
     * getConnection
     * @return Connection
     */
    public static Connection getConnection(){
        if(connection!=null){
            return connection;
        }
        try {
             connection=getDataSource().getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * getQueryRunner
     * @return QueryRunner
     */
    public static QueryRunner getQueryRunner(){
        return new QueryRunner(getDataSource());
    }

    /**
     * closeConnection
     */
    public static void closeConnection(){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }




}
